import type { Phone, PhonePageReq } from '@/type/PhoneType.ts'
import type { ApiPageResp } from '@/type/ApiType.ts'
import { createDiscreteApi } from 'naive-ui'
import { OperatingSystemEnum } from '@/enum/PhoneEnum.ts'
import { log } from 'console'

const { message } = createDiscreteApi(['message'])

export function usePhoneApi() {


  const detailPhone = async (row: any): Promise<Phone> => {
    console.log('detailPhone:row', row);
    return {
      id: row.id,
      categoryId: 2,
      brand: '品牌' + (1),
      model: '型号' + (1),
      releaseDate: new Date(),
      price: Math.floor(Math.random() * 10000),
      color: ['黑色', '白色', '蓝色'][Math.floor(Math.random() * 3)],
      storage: [64, 128, 256][Math.floor(Math.random() * 3)],
      ram: [4, 6, 8][Math.floor(Math.random() * 3)],
      screenSize: [5.5, 6.1, 6.7][Math.floor(Math.random() * 3)],
      batteryCapacity: [3000, 4000, 5000][Math.floor(Math.random() * 3)],
      operatingSystem: OperatingSystemEnum.Android
    }
  }

  const exportPhone = async (params:PhonePageReq,ids?: number[]): Promise<any> => {
    return true
  }

  const importPhone = async (param: FormData): Promise<any> => {
    return true
  }

  const batchDelPhone = async (ids: number[]): Promise<any> => {
    return true
  }
  const deletePhone = async (id: number): Promise<any> => {
    return true
  }
  const savePhone = async (params: PhonePageReq): Promise<any> => {
    return true
  }

  const pagePhone = async (params: PhonePageReq): Promise<ApiPageResp<Phone[]>> => {
    return mock(params)
    /*const response = await fetch('/api/phones')
    const result:ApiResponse<ApiPageResp<Phone[]>> = ApiResponse.convert<ApiPageResp<Phone[]>>(response)
    if (result.isFail()) {
      message.error(result.msg)
    }
    return result.data*/
  }

  function mock(params: PhonePageReq):ApiPageResp<Phone[]> {
    const mockPhones:Phone[] = Array.from({ length: params.size }, (_, i) => ({
      id: i + 1,
      categoryId: i + 2,
      brand: '品牌' + (i + 1),
      model: '型号' + (i + 1),
      releaseDate: new Date(),
      price: Math.floor(Math.random() * 10000),
      color: ['黑色', '白色', '蓝色'][Math.floor(Math.random() * 3)],
      storage: [64, 128, 256][Math.floor(Math.random() * 3)],
      ram: [4, 6, 8][Math.floor(Math.random() * 3)],
      screenSize: [5.5, 6.1, 6.7][Math.floor(Math.random() * 3)],
      batteryCapacity: [3000, 4000, 5000][Math.floor(Math.random() * 3)],
      operatingSystem: OperatingSystemEnum.Android
    }))

    return {
      page: params.page,
      size: params.size,
      total: 10, // 模拟总数量
      data: mockPhones
    }
  }
  return {
    exportPhone,
    importPhone,
    batchDelPhone,
    deletePhone,
    savePhone,
    detailPhone,
    pagePhone,
  }
}

